# coding:utf8
import requests
import json

def get_api_response(base_url, endpoint, method='GET', params=None, jsona=None,headers=None):
    """
    发送HTTP请求到指定的API接口，并返回响应。

    :param base_url: API的基础URL，例如 https://api.example.com
    :param endpoint: 具体的接口路径，例如 /user/{user_id}
    :param method: HTTP请求方法，默认是GET，可传入POST、PUT等其他方法（字符串类型）
    :param params: 请求参数（可选），字典类型，主要用于GET请求传递参数
    :param data: 请求体数据（可选），字典类型，主要用于POST、PUT等请求提交数据
    :param headers: 请求头（可选），字典类型
    :return: requests库的Response对象
    """
    url = base_url + endpoint
    # # 公共参数请求头
    # datas = {'pname':'com.dianzhong.reader',
    #         'channelCode':'wb1009691',
    #         'channelFee':'wb1009691',
    #         'utdidTmp':'A20241216150015492HCy14x',
    #         'version':'7.3.0',
    #         'osv':'34',
    #         'launch':'launcher',
    #         'os':'android',
    #         'model':'V2362A',
    #         'brand':'vivo',
    #         'installTime':'1734329724971',
    #         'language':'Chinese',
    #         'launchNum':'12',
    #         'sex':'1',
    #         'token':'',
    #         'utdid':'',
    #         'userId':'',
    #         'nchid':'',
    #         'mchid':'',
    #         'session1':'',
    #         'session2':'',
    #         'manu':'',
    #         'changeChidDate':'',
    #         'visitor':'0'}
    # json_datas = json.dumps(datas)
    # headers ={
    #         'ueixjkdu': 'euc739shhz7e090394nfnay',
    #         'Content-Type': 'application/json',
	# 	    'datas':json_datas}
    try:
        if method == 'GET':
            response = requests.get(url, params=params, headers=headers)
        elif method == 'POST':
            response = requests.post(url, json=jsona, headers=headers)
        elif method == 'PUT':
            response = requests.put(url, data=data, headers=headers)
        # 可以根据需要继续添加其他请求方法的处理逻辑
        else:
            raise ValueError(f"不支持的请求方法: {method}")
        response.raise_for_status()  # 如果请求失败，抛出异常
        return response
    except requests.RequestException as e:
        print(f"请求接口出现异常: {e}")
        return None

if __name__ == '__main__':
    data = '"bookIdInfos":[ {"bookId": "","chapterId": 0,"isAutoSub": 0,"readDate": 0,"source": ""}], "oftenBookIdList": [{"bookId": "","chapterId": 0,"isAutoSub": 0,"readDate": 0,"source": ""}],"pageFlag": ""}'
    # print(get_api_response('http://asg20.dz.dzods.cn','/asg-portal/portal/client/1101','POST',data=data,headers="application/json"))